package com.ruoyi.control.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.domain.Belt;
import com.ruoyi.control.dto.BeltDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.BeltHistoryMapper;
import com.ruoyi.control.domain.BeltHistory;
import com.ruoyi.control.service.IBeltHistoryService;

/**
 * 皮带历史Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-09
 */
@Service
public class BeltHistoryServiceImpl extends ServiceImpl<BeltHistoryMapper, BeltHistory> implements IBeltHistoryService //修改
{
    @Autowired
    private BeltHistoryMapper beltHistoryMapper;

    @Autowired
    private RedisCache redisCache;


    /**
     * 查询皮带历史
     *
     * @param id 皮带历史主键
     * @return 皮带历史
     */
    @Override
    public BeltHistory selectBeltHistoryById(String id) {
        return beltHistoryMapper.selectBeltHistoryById(id);
    }

    /**
     * 查询皮带历史列表
     *
     * @param beltHistory 皮带历史
     * @return 皮带历史
     */
    @Override
    public List<BeltHistory> selectBeltHistoryList(BeltHistory beltHistory) {
        return beltHistoryMapper.selectBeltHistoryList(beltHistory);
    }

    /**
     * 新增皮带历史
     *
     * @param beltHistory 皮带历史
     * @return 结果
     */
    @Override
    public int insertBeltHistory(BeltHistory beltHistory) {
        return beltHistoryMapper.insertBeltHistory(beltHistory);
    }

    /**
     * 修改皮带历史
     *
     * @param beltHistory 皮带历史
     * @return 结果
     */
    @Override
    public int updateBeltHistory(BeltHistory beltHistory) {
        beltHistory.setUpdateTime(DateUtils.getNowDate());
        return beltHistoryMapper.updateBeltHistory(beltHistory);
    }

    /**
     * 批量删除皮带历史
     *
     * @param ids 需要删除的皮带历史主键
     * @return 结果
     */
    @Override
    public int deleteBeltHistoryByIds(String[] ids) {
        return beltHistoryMapper.deleteBeltHistoryByIds(ids);
    }

    /**
     * 删除皮带历史信息
     *
     * @param id 皮带历史主键
     * @return 结果
     */
    @Override
    public int deleteBeltHistoryById(String id) {
        return beltHistoryMapper.deleteBeltHistoryById(id);
    }

    /**
     * 皮带故障次数  3天、1天
     *
     * @return
     */
    @Override
    public int getBeltTotal() {
        final String key = "pdKey";
        int total = 0;
        String cachedValue = redisCache.getCacheObject(key);

        List<BeltDto> list = beltHistoryMapper.getBeltTotal();
        total = list.size();
        redisCache.setCacheObject(key, String.valueOf(total));

//        if (cachedValue == null || cachedValue.isEmpty()) {
//            List<BeltDto> list = beltHistoryMapper.getBeltTotal();
//            total = list.size();
//            redisCache.setCacheObject(key, String.valueOf(total));
//        } else {
//            total = Integer.parseInt(cachedValue);
//            System.out.println("读取pdKey缓存: " + total);
//        }
        return Integer.parseInt(cachedValue);
    }


}
